//
//  Play_commentView.m
//  SoundPill
//
//  Created by 宋旭东 on 15/6/10.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "Play_commentView.h"

@interface Play_commentView()
{
    NSUInteger start;
}
@end


@implementation Play_commentView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.796 green:0.800 blue:0.800 alpha:1.00];
        [self createButtons];
        start = 0;
    }
    return self;
}
- (void)createButtons
{
    // 评论框
    self.comment = [[UITextField alloc] initWithFrame:CGRectMake(15, 5, self.frame.size.width - 80, self.frame.size.height - 10)];
    self.comment.placeholder = @"输入评论...";
    self.comment.font = [UIFont systemFontOfSize:16];
    self.comment.returnKeyType = UIReturnKeySend;
    self.comment.textColor = [UIColor blackColor];
    self.comment.backgroundColor = [UIColor clearColor];
    self.comment.borderStyle = UITextBorderStyleNone;
    _comment.delegate = self;
    _comment.clearsOnBeginEditing = YES;
    _comment.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    [_comment addTarget:self action:@selector(textFieldDidEditingChanged:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:self.comment];
    
    // 发送按钮
    self.send = [UIButton buttonWithType:UIButtonTypeCustom];
    _send.hidden = YES;
    self.send.frame = CGRectMake(self.frame.size.width- 5 - self.frame.size.height, 0, self.frame.size.height, self.frame.size.height);
    [_send setImage:[UIImage imageNamed:@"Play_comment"] forState:UIControlStateNormal];
    [self addSubview:self.send];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"uid"] == nil) {
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"nologin" object:nil];
        return NO;
    } else {
        _send.hidden = NO;
        return YES;
    }
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}
- (void)textFieldDidEditingChanged:(UITextField *)textField
{
//    [textField.text enumerateSubstringsInRange:NSMakeRange(0, [textField.text length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
//        const unichar hs = [substring characterAtIndex:0];
//        if (0xd800 <= hs && hs <= 0xdbff) {
//            if (substring.length > 1) {
//                const unichar ls = [substring characterAtIndex:1];
//                const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
//                if (0x1d000 <= uc && uc <= 0x1f77f) {
//                    [textField deleteBackward];
//                }
//            }
//        } else if (substring.length > 1) {
//            const unichar ls = [substring characterAtIndex:1];
//            if (ls == 0x20e3) {
//                [textField deleteBackward];
//            }
//        } else {
//            if (0x2100 <= hs && hs <= 0x27ff) {
//                [textField deleteBackward];
//            } else if (0x2B05 <= hs && hs <= 0x2b07) {
//                [textField deleteBackward];
//            } else if (0x2934 <= hs && hs <= 0x2935) {
//                [textField deleteBackward];
//            } else if (0x3297 <= hs && hs <= 0x3299) {
//                [textField deleteBackward];
//            } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
//                [textField deleteBackward];
//            }
//        }
//    }];
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
        return YES;
    } else {
        
        if ([string isEqualToString:@"\n"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"send" object:textField.text];
        } else {
            
        }
        return YES;
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
